package util.nodecloner;

import org.htmlparser.Node;
import org.htmlparser.NodeFactory;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;

public class NodeCloner {

	public static Node clone(Node node2,boolean deep,boolean children){
		
		try {
			
			Node n = (Node)node2.clone();
			
			if (!children){
				
				n.setChildren(new NodeList());
				
			}
			
			if (deep){
				
				n.setChildren(new NodeList());
				
				NodeList c = ChildFinder.getChildren(node2);
				
				for (int i = 0; i < c.size(); i++) {
					
					Node nchild = clone(c.elementAt(i),deep,children);
					
					ChildFinder.getChildren(n).add(nchild);
					
					nchild.setParent(n);
					
				}
							
			}
			
			return n;
			
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	
}
